/*
 * @Author: 詹真琦(legendryztachi@gmail.com)
 * @Date: 2022-03-28 18:17:24
 * @LastEditors: 詹真琦(legendryztachi@gmail.com)
 * @LastEditTime: 2022-04-27 17:54:44
 * @FilePath: \code\cwp-front\pc-collaboration\src\handles\handle-list\summary\modules\custom\helper.js
 * @Description: 自定义按钮一些操作。自定义按钮分为：态度、流程、提交，三大块
 */
//加签
import { addNode } from '@/handles/common/add-node';
import { addNodeMessage } from '@/handles/common/add-node/helper';
//当前会签
import { jointSign } from '../joint-sign';
//多级会签
import { moreSign } from '../more-sign';
//知会
import { addInfoNode } from '../inform';
import createHandles from "@/handles/handle-list/create";
// import emitter from '@/plugins/global-callback';
/**
 * @description: 修改态度
 * @param {Object} params 待修改参数对象
 * @param {String} attitude 态度
 * @param {String} attitudeShowName 态度名称
 * @return {*}
 */
function updateAttitude(params, { attitude, attitudeShowName }) {
    params.comment.attitude = { code: attitude, subject: attitudeShowName };
}

/**
 * @description: 按传入操作执行相应操作
 * @param {String}
 * @return {*}
 */
function executeHandle(handle, params) {
    //加签、当前会签、多级会签、知会
    switch (handle) {
        //加签
        case 'addNode':
            addNode.call(this, params);
            break;
        //当前会签
        case 'jointSign':
            jointSign.call(this, params);
            break;
        //多级会签
        case 'moreSign':
            moreSign.call(this, params);
            break;
        //知会会签 infom。醉了，单词都错的，所以直接写default吧
        default:
            addInfoNode.call(this, params);
    }
}

/**
 * @description: 修改流程
 * @param {Object|null} params 待修改参数对象
 * @param {String} flowAction 操作类型
 * @param {String} flowActionOriginal 操作类型，第一个字母可能大写
 * @param {String|number} id 操作id
 * @return {*}
 */
export async function updateFlow({ flowAction, id, flowActionOriginal }, setIsHideGlobalLoading) {
    const initParams = {};
    //获取加签给某些人的数据。
    await addNodeMessage.call(this, initParams, id);
    //要加个参数。不然弹窗不对(目前只发现：加我的下级部门主管)
    initParams.changeNodeMessage.processAction = flowActionOriginal;

    console.log('flowAction', flowAction, initParams);

    console.log('setIsHideGlobalLoading', setIsHideGlobalLoading);

    //如果继续提交就传入自己的回调。不用全局的
    //需要弹窗选择的
    if (setIsHideGlobalLoading) {
        //由于layer弹窗未提供点击取消按钮的回调。所以就直接先关闭弹窗，
        //resolve了在外边再打开
        setIsHideGlobalLoading();
    }
    //表单控件需要预提交再执行操作
    let userType = initParams.changeNodeMessage?.userType||[];
    if (userType?.includes('FormField')) {
        console.log('createHandles.preSubmit',createHandles.preSubmit);
        await createHandles.preSubmit();
    }
    return new Promise((resolve) => {
        //修改工作流完毕
        //如果
        if (setIsHideGlobalLoading) {
            initParams.callback = () => {
                console.log('加签成功！', workflowModule.getWorkflowParams());
                resolve(workflowModule.getWorkflowParams());
            };
            initParams.callBackFn = initParams.callback;
        }
        executeHandle.call(this, flowAction, initParams);
    });
}

/**
 * @description: 修改提交参数
 * @param {Object} params 待修改参数对象
 * @param {Object} btn 按钮数据
 * @param {Function} setIsHideGlobalLoading 最外层的loading,有继续提交、回退等封装的提交操作的操作才有
 * @return {*}
 */
export default async function (params, btn, setIsHideGlobalLoading) {
    //有态度改态度
    if (btn.attitude) {
        updateAttitude(params, btn);
    }
    //有流程改流程
    if (btn.flowAction) {
        await updateFlow.call(this, btn, setIsHideGlobalLoading);
    }
}
